package com.xqq.lottery.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.xqq.lottery.OnOperationListener;
import com.xqq.lottery.R;
import com.xqq.lottery.R.drawable;
import com.xqq.lottery.R.id;
import com.xqq.lottery.R.layout;
import com.xqq.lottery.adapter.FaceCategroyAdapter;
import com.xqq.lottery.adapter.FunctionAdapter;
import com.xqq.lottery.bean.Option;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MessageInputToolBox extends RelativeLayout {

	private static final String TAG ="MessageInputToolBox";
	
	private OnOperationListener onOperationListener;
	
	private Map<Integer, ArrayList<String>> faceData;
	private List<Option> functionData;
	
	private EditText messageEditText;
	private Button faceButton;
	private Button sendButton;
	private Button moreTypeButton;
	
	private RelativeLayout bottomHideLayout;
	private static RelativeLayout faceLayout;
	private ViewPager faceCategroyViewPager;
	
	private LinearLayout moreTypeLayout;
	private ViewPager fuctionViewPager;
	private LinearLayout pagePointLayout;
	List<View> functionGridViewList;
	List<ImageView> pointViews;
	
	FaceCategroyAdapter faceCategroyAdapter;
	
	private Context context;
	private FragmentManager fragmentManager;
	

	public MessageInputToolBox(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		FragmentActivity fragmentActivity = (FragmentActivity) context;
		this.fragmentManager = fragmentActivity.getSupportFragmentManager();
		LayoutInflater.from(context).inflate(R.layout.message_input_tool_box, this);
	}

	public MessageInputToolBox(Context context, AttributeSet attrs) {
		this(context,attrs,0);
	}

	public MessageInputToolBox(Context context) {
		this(context,null);
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		this.initView();
	}
	
	private void initView(){
		messageEditText 		= (EditText) findViewById(R.id.messageEditText);
		sendButton 				= (Button) findViewById(R.id.sendButton);

		bottomHideLayout		= (RelativeLayout) findViewById(R.id.bottomHideLayout);
		faceButton				= (Button) findViewById(R.id.faceButton);
		moreTypeButton			= (Button) findViewById(R.id.moreTypeButton);
		moreTypeLayout			= (LinearLayout) findViewById(R.id.moreTypeLayout);
		
		faceLayout				= (RelativeLayout) findViewById(R.id.faceLayout);
		faceCategroyViewPager 	= (ViewPager) findViewById(R.id.faceCategroyViewPager);
		
		fuctionViewPager = (ViewPager) findViewById(R.id.fuctionViewPager);
		pagePointLayout = (LinearLayout) findViewById(R.id.pagePointLayout);
		fuctionViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				for(int i=0;i<pointViews.size();i++){
					if (arg0 == i) {
						pointViews.get(i).setBackgroundResource(R.drawable.point_selected);
					} else {
						pointViews.get(i).setBackgroundResource(R.drawable.point_normal);
					}
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		
		faceCategroyAdapter =new FaceCategroyAdapter(fragmentManager);
		sendButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(onOperationListener != null && !"".equals(messageEditText.getText().toString().trim())){
					String content = messageEditText.getText().toString();
					onOperationListener.send(content);
					messageEditText.setText("");
				}
			}
		});
		
		faceButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(faceLayout.getVisibility() == VISIBLE){
					showKeyboard(context);
					hideFaceLayout();
				}else{
					showFaceLayout();
				}
			}
		});
		
		messageEditText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				hideFaceLayout();
			}
		});
		
		messageEditText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s != null && !"".equals(s.toString().trim())){
					moreTypeButton.setVisibility(View.GONE);
					sendButton.setEnabled(true);
					sendButton.setVisibility(View.VISIBLE);
				}else{
					moreTypeButton.setVisibility(View.VISIBLE);
					if(moreTypeButton.getVisibility() == View.VISIBLE){
						sendButton.setVisibility(View.GONE);
					}
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});
		
		moreTypeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showFunctionLayout();
			}
		});
		
	}
	
	public void showFaceLayout(){
		hideKeyboard(this.context);
		moreTypeLayout.setVisibility(View.GONE);
		faceLayout.setVisibility(View.VISIBLE);
		bottomHideLayout.setVisibility(View.VISIBLE);
	}
	
	public void hideFaceLayout(){
		moreTypeLayout.setVisibility(View.GONE);
		faceLayout.setVisibility(View.GONE);
		bottomHideLayout.setVisibility(View.GONE);
	}
	
	public void showFunctionLayout(){
		hideKeyboard(context);
		faceLayout.setVisibility(GONE);
		bottomHideLayout.setVisibility(VISIBLE);
		moreTypeLayout.setVisibility(VISIBLE);
	}
	
	public void hideFunctionLayout(){
		bottomHideLayout.setVisibility(GONE);
		faceLayout.setVisibility(GONE);
		moreTypeLayout.setVisibility(GONE);
	}
	
	public void hide(){
		bottomHideLayout.setVisibility(View.GONE);
		hideKeyboard(context);
	}
	
	
	public boolean isBottomVisibility(){
		return bottomHideLayout.getVisibility() == View.VISIBLE;
	}
	
	public static void hideKeyboard(Context context){
		Activity activity = (Activity) context;
		if(activity != null){
			InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		    if(imm.isActive() && activity.getCurrentFocus() != null){
		    	imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
		    }
		}
	}
	
	public static void showKeyboard(Context context){
		Activity activity = (Activity) context;
		if(activity != null){
			InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
			imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}
	
	public OnOperationListener getOnOperationListener(){
		return onOperationListener;
	}
	public void setOnOperationListener(OnOperationListener listener){
		this.onOperationListener = listener;
		faceCategroyAdapter.setOnOperationListener(listener);
	}
	
	public Map<Integer,ArrayList<String>> getFaceData(){
		return faceData;
	}
	
	public void setFaceData(Map<Integer, ArrayList<String>> faceData){
		this.faceData = faceData;
		faceCategroyAdapter.setData(faceData);
		faceCategroyViewPager.setAdapter(faceCategroyAdapter);;
	}
	
	public List<Option> getFunctionData(){
		return functionData;
	}
	
	public void setFunctionData(List<Option> functionData){
		this.functionData = functionData;
		pointViews = new ArrayList<ImageView>();
		functionGridViewList = new ArrayList<View>();
		
		int funSize = functionData.size();
		for(int x = 0;x<(funSize % 8 == 0?funSize/8:funSize/8+1);x++){
			GridView view = new GridView(context);
			FunctionAdapter functionAdapter =new FunctionAdapter(context,
				functionData.subList(x*8, ((x+1)*8 > funSize?funSize:(x+1)*8)));
		    view.setAdapter(functionAdapter);
		    
		    view.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					if(onOperationListener != null){
						onOperationListener.selectedFuncation(position);
					}
				}
			});
		    view.setNumColumns(4);
		    view.setBackgroundColor(Color.TRANSPARENT);
		    view.setHorizontalSpacing(1);
		    view.setVerticalSpacing(1);
		    view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
		    view.setCacheColorHint(0);
		    view.setPadding(5, 0, 5, 0);
		    view.setSelector(new ColorDrawable(Color.TRANSPARENT));
		    view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
		    		LayoutParams.WRAP_CONTENT));
		    view.setGravity(Gravity.CENTER);
		    functionGridViewList.add(view);
		    ImageView imageView =new ImageView(context);
		    imageView.setBackgroundResource(R.drawable.point_normal);
		    LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(
		    		new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		    layoutParams.leftMargin = 10;
		    layoutParams.rightMargin = 10;
		    layoutParams.width = 8;
		    layoutParams.height = 8;
		    pagePointLayout.addView(imageView,layoutParams);
		    if(x == 0){
		    	imageView.setBackgroundResource(R.drawable.point_selected);;
		    }
		    pointViews.add(imageView);
		}
		
		fuctionViewPager.setAdapter(new PagerAdapter() {
			
			@Override
			public int getCount() {
				return functionGridViewList.size();
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getItemPosition(Object object) {
				return super.getItemPosition(object);
			}

			@Override
			public void destroyItem(View arg0, int arg1, Object arg2) {
				((ViewPager) arg0).removeView(functionGridViewList.get(arg1));
			}

			/***
			 * 获取每一个item?类于listview中的getview
			 */
			@Override
			public Object instantiateItem(View arg0, int arg1) {
				((ViewPager) arg0).addView(functionGridViewList.get(arg1));
				return functionGridViewList.get(arg1);
			}
			
		});
	}
	
	

}
